
#include <msr.h>

/**
 * Read the CR0 register
 * 
 * returns - CR0
 */
dword inline msr_read_cr0(void)
{
	dword ret;
	asm volatile("movl %%cr0, %0" : "=r"(ret));
	return ret;
}

/**
 * Write to the CR0 register
 * 
 * dword cr0 - new CR0 value
 */
void inline msr_write_cr0(dword cr0)
{
	asm volatile("movl %0, %%cr0" : : "r"(cr0));
}

/**
 * Read the CR3 register
 * 
 * returns - CR3
 */
dword inline msr_read_cr3(void)
{
	dword ret;
	asm volatile("movl %%cr3, %0" : "=r"(ret));
	return ret;
}

/**
 * Write to the CR3 register
 * 
 * dword cr3 - new CR3 value
 */
void inline msr_write_cr3(dword cr3)
{
	asm volatile("movl %0, %%cr3" : : "r"(cr3));
}

/**
 * Read an MSR
 * 
 * dword msr - MSR to read
 * dword *lo - low dword of MSR value
 * dword *hi - high dword of MSR value
 */
void inline msr_read(dword msr, dword *lo, dword *hi)
{
   asm volatile("rdmsr":"=a"(*lo),"=d"(*hi) : "c"(msr));
}

/**
 * Write to an MSR
 * 
 * dword msr - MSR to write to
 * dword lo - low dword of new MSR value
 * dword hi - high dword of new MSR value
 */
void inline msr_write(dword msr, dword lo, dword hi)
{
   asm volatile("wrmsr":"=a"(lo),"=d"(hi) : "c"(msr));
}
